import { Col, Descriptions, Form, Input, Modal, Row } from 'antd';
import { useState, useEffect } from 'react'
import FetchHelper from '../../../../../../utils/helper/fetch-helper';
export default function EditModal(props) {
    const { record, jzzyxlDefs, node } = props;
    const [form] = Form.useForm();
    const [loading, setLoading] = useState();
    console.log(node)
    useEffect(() => {
        if (record) {
            form.setFieldsValue(record);
        }
    }, [form, record])

    if (!jzzyxlDefs?.templates) {
        return null
    }
    return <Modal confirmLoading={loading} width={1000} onOk={e => {
        form.validateFields().then(values => {
            const data = {
                ...record,
                ...values,
                jzzydl: "减灾资源",
                jzzyzl: node.jzzyzl,
                jzzyxl: node.title
            }
            if (record) {
                setLoading(true);
                FetchHelper.putJson(`/admin-api/ybyj/jzzy`, {
                    ...data,
                    xq: JSON.stringify(data)
                }).then(resp => {
                    props.onOk?.()
                }).finally(e => {
                    setLoading(false);
                })
            } else {
                setLoading(true);
                FetchHelper.postJson(`/admin-api/ybyj/jzzy`, {
                    ...data,
                    xq: JSON.stringify(data)
                }).then(resp => {
                    props.onOk?.()
                }).finally(e => {
                    setLoading(false);
                })
            }

        })
    }} bodyStyle={{
        height: 600,
        padding: 24
    }} title={`${record ? "编辑" : "新增"}${node.title}`} open={props.open} onCancel={e => props.onCancel?.()}>
        <Form form={form} labelAlign='right' labelCol={{ span: 6 }} labelWrap={{ span: 18 }}>
            <Row gutter={24}>
                {jzzyxlDefs?.templates?.map(item => {
                    let inputNode = <Input />
                    switch (item.type) {
                        case 'map':
                        default:
                    }
                    return <Col span={12}>
                        <Form.Item rules={[{
                            required: item.required
                        }]} label={item.title} name={item.dataIndex} key={item.dataIndex}>
                            {inputNode}
                        </Form.Item>
                    </Col>
                })}
            </Row>
        </Form>
    </Modal>;
}